package com.ajie.entity;

import com.ajie.utils.StringUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户实体
 */

@Data
@Slf4j
public class SysUser implements UserDetails {
    @ApiModelProperty(value = "主键")
    private Long id;

    //p52 7:55有redis忽视标签
    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "前端展示的用户名")
    private String name;
    
    @ApiModelProperty(value = "姓名")
    private String nickName;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "性别")
    private Integer sex;

    @ApiModelProperty(value = "头像")
    private String avatar;

    @ApiModelProperty(value = "地址")
    private String address;

    @ApiModelProperty(value = "微信小程序id")
    private String openId;

    @ApiModelProperty(value = "是否被禁用")
    private boolean status;

    @ApiModelProperty(value = "手机号")
    private String phoneNumber;
    
    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "角色信息")
    private List<SysRole> roles;

    /**
     * 角色对应的菜单
     */
    @ApiModelProperty(value = "用户对应的菜单")
    private List<SysMenu> menus;

    /**
     * 角色对应数据权限
     */
    @ApiModelProperty(value = "用户对应数据权限")
    private List<SysPermission> permissions;


    /**
     * 获取权限数据
     * @return
     */
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        log.info("该文件名是：SysUser,该方法是：getAuthorities");
        List<GrantedAuthority> list = new ArrayList<>();
        if (roles != null && roles.size()>0 ){
            roles.forEach(item -> {//代码健壮性判断
                if (StringUtil.isNotEmpty(item.getCode())){
                    list.add(new SimpleGrantedAuthority("ROLE_" + item.getCode()));
                }
            });
        }
        if (permissions != null && permissions.size()>0){
            permissions.forEach(item ->{
                if (StringUtil.isNotEmpty(item.getCode())){
                    list.add(new SimpleGrantedAuthority(item.getCode()));
                }
            } );
        }
        return list;
    }
   
    
    
    
    /**
     * 账号是否过期
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        log.info("该文件名是：SysUser,该方法是：isAccountNonExpired");
        return false;
    }

    /**
     * 账号是否被被锁定
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        log.info("该文件名是：SysUser,该方法是：isAccountNonLocked");
        return false;
    }

    /**
     *
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        log.info("该文件名是：SysUser,该方法是：isCredentialsNonExpired");
        return false;
    }

    /**
     * 账号是否被禁用
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        log.info("该文件名是：SysUser,该方法是：isEnabled");
        return status;
    }
}
